﻿using Api.Core.IRepository.Module;
using Api.Core.IServices.Module;
using Api.Core.IServices.Role;
using Api.Core.Model.Module;
using Api.Core.Model.Role;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api.Core.Services.Module
{
    public class ModuleInfoService : BaseService<ModuleEntity>, IModuleInfoService
    {
        private readonly IRoleModuleInfoService _roleModuleInfoService;

        public ModuleInfoService(IModuleRepository moduleRepository
            , IRoleModuleInfoService roleModuleInfoService)
        {
            _roleModuleInfoService = roleModuleInfoService;
            base.BaseDal = moduleRepository;
        }




        public async Task<List<ModuleEntity>> GetMenus(string userId)
        {
            var RoleMenuList = await _roleModuleInfoService.GetModuleIds(userId);
            var MenuList = await base.Query();
            var list = MenuList.Where(a => RoleMenuList.Where(t => a.Id == t.ModuleId).Any()).ToList();
            return list;
        }


        public async Task<bool> AddRoleModule(int id, string ids)
        {
            var roleModule = await _roleModuleInfoService.Query(x => x.RoleId == id);
            int[] arrId = roleModule.Select(x => x.Id).ToArray();
            await _roleModuleInfoService.DeleteByIds(arrId);
            List<RoleModuleEntity> roleModuleEntities = new List<RoleModuleEntity>();
            var arr = ids.Split(',');
            foreach (var item in arr)
            {
                roleModuleEntities.Add(new RoleModuleEntity() { RoleId = id, ModuleId = int.Parse(item) });
            }
            return await _roleModuleInfoService.Add(roleModuleEntities) > 0;

        }
    }
}
